package com.atguigu.study.base.cas;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLongArray;

/**
 * @Author 快点干早点散
 * @Create 2025/4/24 20:48
 * Description:
 */
public class CASDemo2 {

    AtomicInteger atomicInteger = new AtomicInteger(0);

    public void setAtomicInteger() {
         atomicInteger.incrementAndGet();
    }
    public  int getAtomicInteger(){
        return  atomicInteger.get();
    }

    public static void main(String[] args) {
        CASDemo2 casDemo2 = new CASDemo2();
        CountDownLatch countDownLatch=new CountDownLatch(100);

        for (int i = 1; i <= 100; i++) {
            new Thread(() -> {
                for (int i1 = 1; i1 <= 100; i1++) {
                casDemo2.setAtomicInteger();
                }
                countDownLatch.countDown();
            },String.valueOf(i)).start();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println("casDemo2 = " + casDemo2.atomicInteger.getAndIncrement());
    }
}
